//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Claim.h"
using namespace Cephei::QL::Instruments;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CClaim::CClaim (boost::shared_ptr<QuantLib::Claim>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phClaim = NULL;
#endif
	_ppClaim = &childNative;
    
}
Cephei::QL::Instruments::CClaim::CClaim (QuantLib::Claim& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phClaim = NULL;
#endif
	_ppClaim = new boost::shared_ptr<QuantLib::Claim> (&childNative);
    
    _ClaimOwner = owner;
    
}

Cephei::QL::Instruments::CClaim::CClaim (CClaim^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phClaim = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppClaim = new boost::shared_ptr<QuantLib::Claim> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::CClaim::CClaim (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phClaim = NULL;
#endif
	if (!t->IsSubclassOf(CClaim::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CClaim::CClaim (QuantLib::Handle<QuantLib::Claim>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phClaim = &childNative;
	_ppClaim = &static_cast<boost::shared_ptr<QuantLib::Claim>>(childNative.currentLink());
    
    _ClaimOwner = owner;
}
Cephei::QL::Instruments::CClaim::CClaim (QuantLib::Handle<QuantLib::Claim> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phClaim = &childNative;
	_ppClaim = &static_cast<boost::shared_ptr<QuantLib::Claim>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CClaim::CClaim (QuantLib::Claim childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phClaim = NULL;
#endif
	_ppClaim = new boost::shared_ptr<QuantLib::Claim> (new QuantLib::Claim (childNative));
    
}
#endif

Cephei::QL::Instruments::CClaim::~CClaim ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppClaim != NULL)
    {
	    delete _ppClaim;
        _ppClaim = NULL;
    }
}
Cephei::QL::Instruments::CClaim::!CClaim ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppClaim != NULL)
    {
	    delete _ppClaim;
    }
}
QuantLib::Claim& Cephei::QL::Instruments::CClaim::GetReference ()
{
    if (_ppClaim == NULL) throw gcnew NativeNullException ();
	return **_ppClaim;
}
boost::shared_ptr<QuantLib::Claim>& Cephei::QL::Instruments::CClaim::GetShared ()
{
    if (_ppClaim == NULL) throw gcnew NativeNullException ();
	return *_ppClaim;
}
QuantLib::Claim* Cephei::QL::Instruments::CClaim::GetPointer ()
{
    if (_ppClaim == NULL) throw gcnew NativeNullException ();
	return &**_ppClaim;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Claim>& Cephei::QL::Instruments::CClaim::GetHandle ()
{
	if (_phClaim == NULL)
	{
		_phClaim = new Handle<QuantLib::Claim> (*_ppClaim);
	}
	return *_phClaim;
}
#endif
bool Cephei::QL::Instruments::CClaim::HasNative () 
{
	return (_ppClaim != NULL);
}

Double Cephei::QL::Instruments::CClaim::Amount (DateTime defaultDate, Double notional, Double recoveryRate)
{
    try
    {
        QuantLib::Date _defaultDate = (QuantLib::Date)ValueHelper::Convert (defaultDate);
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional);
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppClaim)->amount ( _defaultDate,  _notional,  _recoveryRate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IClaim^ Cephei::QL::Instruments::CClaim::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppClaim)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

